const app = getApp();
import util from "../../../../utils/util";
var QQMapWX = require('../../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
  key: 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'
});
Page({
  data: {
    cityname: "深圳",
    citycode: "sz",
    selectType: [{
      type: "停电",
      img: "http://imgbdb3.bendibao.com/whbdb/202011/10/20201110163435_64496.jpg"
    }, {
      type: "停水",
      img: "http://imgbdb3.bendibao.com/whbdb/202011/10/20201110163445_47785.jpg"
    }, {
      type: "停气",
      img: "http://imgbdb3.bendibao.com/xcxbdb/file/20213/8/20210308114410_13270.jpeg",
    }],
    typeIndex: 0,
    infoList: [],
  },

  onLoad: function (options) {
    this.setData({
      typeIndex: options.type <= 2 ? options.type : 0,
      citycode: options.citycode ? options.citycode : wx.getStorageSync('citycode') ? wx.getStorageSync('citycode') : 'sz',
      keywords: options.qu ? options.qu : '',
    });
    util.getUrl(app.globalData.url).then(() => {
      util.request("zhuanti.php", {
        action: "cut_off",
        category: this.data.selectType[this.data.typeIndex].type,
        type: "city"
      }, "POST").then(res => {
        if (res.data.state == 1) {
          let key = this.data.citycode.substr(0, 1).toUpperCase();
          if (res.data.data.city[key] && res.data.data.city[key].find(city => city.citycode == this.data.citycode))
            this.setData({
              cityname: res.data.data.city[key].filter(city => city.citycode == this.data.citycode)[0].cityname,
            });
        }
        this.getLocation()
      });
    });

  },
  // 获取定位
  getLocation() {
    qqmapsdk.reverseGeocoder({
      success: (res) => {
        let city = res.result.address_component.city;
        if (city && !city.includes(this.data.cityname)) {
          wx.showModal({
            content: '您当前定位城市为' + city + '\n是否需要切换到' + city + this.data.selectType[this.data.typeIndex].type + '查询？',
            success: async res => {
              if (res.confirm) {
                city = city.substr(0, city.length - 1);
                const cityCode = await this.getCityCode(city)
                this.setData({
                  cityname: city,
                  citycode: cityCode,
                })
                this.getInfo()
              }
            }
          })
        }
      }
    })
  },
  // 获取cityCode,参数为市
  async getCityCode(city) {
    const res = await util.request("citycode.php", {
      action: "city",
      city: city
    }, "POST")

    return res.data.data[0].citycode
  },
  onShow() {
    this.getInfo();
  },

  // 获取查询信息
  getInfo() {
    util.request("zhuanti.php", {
      action: "cut_off",
      category: this.data.selectType[this.data.typeIndex].type,
      citycode: this.data.citycode,
      keywords: this.data.keywords,
    }, "POST").then(res => {
      if (res.data.state == 1)
        if (res.data.data.cut)
          res.data.data.cut.forEach(item => {
            item.show = false;
            // if (item.begin_time) {
            //     let begin_time = new Date(item.begin_time);
            //     item.begin_time = `${begin_time.getMonth() + 1}月${begin_time.getDate()}日 ${begin_time.getHours()}:${begin_time.getMinutes()}`
            // }

            // if (item.end_time) {
            //     let end_time = new Date(item.end_time);
            //     item.end_time = `${end_time.getMonth() + 1}月${end_time.getDate()}日 ${end_time.getHours()}:${end_time.getMinutes()}`
            // }
          });
      this.setData({
        infoList: res.data.data,
      });

    });
  },

  typeChange(e) {
    let index = e.detail.value;
    this.setData({
      typeIndex: index,
      infoList: [],
    });
    this.getInfo();
  },

  shwoText(e) {

    let index = util.getValue("index", e);
    let data = this.data.infoList.cut[index];
    data.show = !data.show;
    this.setData({
      infoList: this.data.infoList,
    });
  },

  inputKeyWrods(e) {
    let keywords = e.detail.value;
    this.setData({
      keywords,
    });
  },

  select() {
    this.getInfo();
  },

  onShareAppMessage: function () {

  }
});